package cn.com.callback

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import cn.com.elite.EliteBean;
import cn.com.elite.PropertiesUtil;
import cn.com.elite.Util;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONObject;
/**
 * 
 * @author msjykf
 * 安正数据字典同步接口
 *
 */
public class DictSync implements ServiceBackInvoke{
	private static final Log log=LogFactory.getLog(DictSync.class);
	public String doCallBack(EliteBean eliteBean, HttpServletRequest request,HttpServletResponse response){
		try {
			def jsonbody = Util.getRequestBody(request,'utf-8'); //获取报文
			JSONObject obj = JSONObject.fromObject(jsonbody);
			def dict_type = Util.filterForObject(obj.get("dict_type")); //数据字典类型
			PropertiesUtil properties = new PropertiesUtil();
			def ibqurl = properties.readValue("ibqurl"); //获取webservice接口地址
			def appid = properties.readValue("appid"); //获取appId
			def params = ""; //查询参数
			def smap,findrow;
			if(!dict_type.equals("")) {
				smap = eliteBean.chaxun_sql("select count(*) as findrow from  custom_parameter_type where parameter_type='"+dict_type+"' and datasource=2","dataSource");
				findrow = smap[0].get("findrow");
				if(findrow==0) {
					return "{\"msg\": \"请维护数据字典类型！\",\"code\": -2}"
				};
				params = """
									<params>
										<name>DICT_TYPE</name>
									 	<operator>equal</operator>
									 	<value>"""+dict_type+"""</value>
									</params>""";
			};
			params = """<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
							<SOAP-ENV:Body>
								<m:query xmlns:m="http://service.web.server.ua.businessmatrix.com/">
									<appId>"""+appid+"""</appId>
									<name>s-DICT</name>"""+params+"""
								</m:query>
							</SOAP-ENV:Body>
						</SOAP-ENV:Envelope>"""
			def charset = "utf-8"; //设置字符集
			def timeout = 100000; //设置请求超时时间
			def enableSSLflag = 0;
			def retrunxml = Util.callWebService(ibqurl,params,charset,"",timeout);
			retrunxml = Util.Transform(retrunxml);
			log.debug("retrunxml:"+retrunxml);
			def list = new XmlParser().parseText(retrunxml);
			int c1,c2,c3,c4;
			def i = 0;
			for(it in list.Body.queryResponse.QueryResult.columns) {
				log.debug("it:"+it.name.text());
				switch(it.name.text()) {
					case 'DICT_TYPE':
					c1 = i;
					break;
					case 'DICT_CODE':
					c2 =i;
					break;
					case 'DICT_DESC':
					c3 =i;
					break;
					case 'P_DICT_CODE':
					c4 =i;
					break;
				};
				i++;
			};
			def dict_code,dict_desc,p_dict_code;
			smap = eliteBean.chaxun_sql("select nvl(max(seq),0) maxnum from  custom_parameter where parameter_type='"+dict_type+"'","dataSource");
			def maxnum = smap[0].get("maxnum");
			for(it in list.Body.queryResponse.QueryResult.rows) {
				try {
					maxnum++;
					dict_type = it.item[c1].text();
					dict_code = it.item[c2].text();
					dict_desc = it.item[c3].text();
					p_dict_code = it.item[c4].text();
					smap = eliteBean.chaxun_sql("select count(*) as findrow from custom_parameter where parameter_id='"+dict_code+"' and parameter_type='"+dict_type+"'","dataSource");
					findrow = smap[0].get("findrow");
					if(findrow==0) {
						eliteBean.update("insert into custom_parameter(parameter_guid,parameter_id,parameter_desc,parameter_type,seq,userflag,parameter_groupid) values(sysguid,'"+dict_code+"','"+dict_desc+"','"+dict_type+"',"+maxnum+",1,'"+p_dict_code+"')","dataSource");
					}else {
						eliteBean.update("update custom_parameter set parameter_desc='"+dict_desc+"',parameter_groupid='"+p_dict_code+"'where parameter_id='"+dict_code+"' and parameter_type='"+dict_type+"'","dataSource");
					};
				}catch(Exception e) {
					log.error("ErrorMessage:",e);
				};	
			};
			return "{\"msg\": \"请求接口成功！\",\"code\": 1}"
		}catch(Exception e) {
			log.error("ErrorMessage:",e);
			return "{\"msg\": \"请求接口失败！\",\"code\": -1}"
		}
	}	
}